package com.ljw.hmall.item.web;

import com.ljw.hmall.dto.PageDTO;
import com.ljw.hmall.item.pojo.Item;
import com.ljw.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO<Item> selectByPage(Integer page,Integer size){
        PageDTO<Item> pageDTO=itemService.selectPages(page,size);
        return pageDTO;
    }

    @GetMapping
    public List<Item> selectAll(){
        return itemService.list();
    }


    @GetMapping("/{id}")
    public Item selectById(@PathVariable Long id){
       Item item=itemService.selectOne(id);
        return item;
    }

    @PostMapping
    public void insert(@RequestBody Item item){
        log.info("item:{}",item);
       itemService.insertOne(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void update(@PathVariable Long id , @PathVariable Integer status){
        log.info("id:{},status:{}",id,status);
       itemService.updateStatus(id,status);
    }

    @PutMapping
    public void updateById(@RequestBody Item item){
        log.info("item:{}",item);
        itemService.updateById(item);
    }


    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        log.info("id:{}",id);
        itemService.removeById(id);
    }


    @GetMapping("/rid")
    public Item getById(Long itemId){
        Item item = itemService.selectOne(itemId);
        return item;
    }

    @PutMapping("/stock/{itemId}/{num}")
    public void updateById( @PathVariable Long itemId,@PathVariable Integer num){
        itemService.updateStock(itemId,num);
    }

    @PutMapping("/stock/recover/{itemId}/{num}")
    public void updateAddStock( @PathVariable Long itemId,@PathVariable Integer num){
        itemService.addStock(itemId,num);
    }
}
